package com.tuju.jetpackfirstdemo.base.baseadapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.tuju.jetpackfirstdemo.R
import kotlinx.android.synthetic.main.item_demo.view.*
import kotlinx.android.synthetic.main.item_loadmore_footer.view.*

class LoadMoreAdapter<T> : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val data = ArrayList<T>()
        private var loadMoreListener: LoadMoreListener? = null

    //是否开启上拉加载更多,默认不开启
    private var loadMoreEnable = false

    private var hasMoreData = true

    private var mLoading: Boolean = false

    //=========private=========
    private val NORMAL_ITEM = 0
    private val FOOTER_ITEM = 1

    override fun getItemCount(): Int = if (loadMoreEnable) data.size + 1 else data.size

    override fun getItemViewType(position: Int): Int {
        if (loadMoreEnable) {
            if (position == data.size) {
                return FOOTER_ITEM
            }
        }
        return NORMAL_ITEM
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == NORMAL_ITEM) {
            NormalViewHolder(getView(parent, R.layout.item_demo))
        } else {
            FooterViewHolder(getView(parent, R.layout.item_loadmore_footer))
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (loadMoreEnable) {
            if (position == data.size) {//加载更多布局
                (holder as FooterViewHolder).onSetData(hasMoreData)
                autoLoadMore(position)
                return
            }
        }
        //其他正常布局
        val bean = data[position] as String
        (holder as NormalViewHolder).onSetData(bean)
    }

    //自动加载更多
    private fun autoLoadMore(position: Int) {
        if (!hasMoreData) {
            return
        }
        if (!mLoading) {
            mLoading = true
            loadMoreListener?.onLoadMoreLoading()
        }
    }

    fun loadMoreComplete(){
        hasMoreData = true
        mLoading = false
        notifyItemChanged(data.size)
    }

    protected fun getView(parent: ViewGroup, layoutId: Int) = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)

    class NormalViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
        fun onSetData(bean: String) {
            view.tv_demo_str.text = bean
        }
    }

    class FooterViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        fun onSetData(hasMoreData: Boolean) {
            if (hasMoreData) {
                view.tv_loadmore_tips.text = "正在加载..."
            } else {
                view.tv_loadmore_tips.text = "没有更多数据了~"
            }
        }

    }

    interface LoadMoreListener {
        fun onLoadMoreLoading()
    }

    fun setLoadMoreListener(loadMoreListener: LoadMoreListener) {
        this.loadMoreListener = loadMoreListener
    }

    fun setLoadMoreEnable(loadMoreEnable: Boolean) {
        this.loadMoreEnable = loadMoreEnable
    }

    fun noMoreLoading() {
        this.hasMoreData = false
        notifyItemChanged(data.size)
    }

    fun reset() {
        this.hasMoreData = true
        notifyItemChanged(data.size)
    }

    fun add(bean: T) {
        data.add(bean)
    }

    fun addAll(lists: List<T>) {
        data.addAll(lists)
    }

    fun addAll(position: Int, lists: List<T>) {
        data.addAll(position, lists)
    }

    fun clear() {
        data.clear()
    }

    fun getData() = data

}